package com.example.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.vo.ResultVo;
import com.github.pagehelper.PageInfo;
import com.example.pojo.Position;
import com.example.service.IPositionService;
import io.swagger.annotations.Api;

import java.sql.Date;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 岗位表 前端控制器
 * </p>
 *
 * @author wsl
 * @since 2022-04-26
 */
@RestController
@RequestMapping("/sys/position")
@Api(tags = "岗位表",value = "")
public class PositionController {

    @Autowired
    private IPositionService positionService;

    @ApiOperation("查询部门管理人员")
    @GetMapping("/dept-manage")
    public ResultVo getDeptManage(){
        List<Map<String, Object>> list = positionService.selectDeptByEmp();
        if (!list.isEmpty()){
            return ResultVo.response(list);
        }
        return ResultVo.error();
    }

    @ApiOperation(value = "获取所有岗位选项")
    @GetMapping("/options")
    public ResultVo getPositionOptions() {
        QueryWrapper<Position> wrapper = new QueryWrapper<Position>();
        wrapper
                .select("id as value,name as label,enabled as enabled");
        List<Map<String, Object>> maps = positionService.listMaps(wrapper);
        return ResultVo.response(maps);
    }

    @ApiOperation("根据部门查询岗位")
    @GetMapping("/bydept")
    public ResultVo getPosByDeptId(@RequestParam("did") Integer did){
        System.err.println(" - < " + did);
        if (did != 0){
            List<Position> list = positionService.list(new QueryWrapper<Position>().eq("dept_id", did));
            return ResultVo.response(list);
        }
        return ResultVo.error();
    }


    @ApiOperation(value = "查询岗位表")
    @GetMapping("/query")
    public PageInfo<Position> getPositionPageInfo(
            String keyword,
            Integer status,
            Date start,
            Date end,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return positionService.getPositionPageInfo(pageNum,pageSize,keyword,status,start,end);
    }

    @ApiOperation(value = "新增岗位表")
    @PostMapping("/add")
    public ResultVo addPosition(@RequestBody Position position) {
        if (positionService.save(position)){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    @ApiOperation(value = "修改岗位表")
    @PutMapping("/update")
    public ResultVo updatePosition(@RequestBody Position position) {
        if (positionService.updateById(position)) {
            return ResultVo.success("修改成功。");
        }
        return ResultVo.error("修改失败!");
    }

    @ApiOperation(value = "删除岗位表")
    @DeleteMapping("/delete")
    public ResultVo deletePosition(Integer[] ids){
        if(positionService.removeByIds(Arrays.asList(ids))){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    @ApiOperation(value = "查询是否存在相同名称的岗位")
    @GetMapping("/has-some-name")
    public Boolean hasSomeNamePosition(Integer id,@RequestParam(name = "name")String name){
        return positionService.hasSomeNamePosition(id,name);
    }

    @ApiOperation(value = "查询是否存在相同编码的岗位")
    @GetMapping("/has-some-code")
    public Boolean hasSomeCodePosition(Integer id,@RequestParam(name = "code")String code){
        return positionService.hasSomeCodePosition(id,code);
    }
}
